<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%share}}".
 *
 * @property int $id
 * @property string $name 姓名
 * @property int $start 开跑时间
 * @property string $ban_result 半马成绩
 * @property int $ban_num 半马个数
 * @property string $q_result 半马成绩
 * @property int $q_num 半马个数
 * @property int $num 城市数量
 * @property int $created_at 添加时间
 */
class Share extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%share}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['start', 'ban_num', 'q_num', 'num', 'created_at'], 'integer'],
            [['name', 'ban_result', 'q_result'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'start' => 'Start',
            'ban_result' => 'Ban Result',
            'ban_num' => 'Ban Num',
            'q_result' => 'Q Result',
            'q_num' => 'Q Num',
            'num' => 'Num',
            'created_at' => 'Created At',
        ];
    }

    public function getStars()
    {
        $old = $this->ban_num + $this->q_num;
        if ($old <= 4) {
            return 1;
        } elseif ($old > 4 && $old <= 9) {
            return 2;
        } elseif ($old > 9 && $old <= 14) {
            return 3;
        } elseif ($old > 14 && $old <= 19) {
            return 4;
        } elseif ($old > 19) {
            return 5;
        }
    }

    public function getOld()
    {
        $old = date('Y') - date('Y', $this->start);
        if ($old <= 0) {
            $old = 1;
        }
        return $old;
    }

    public function getMileage()
    {
        $km = 42.195 * $this->q_num + 21.0975 * $this->ban_num;
        return round($km, 2);
    }

}
